package com.hkyl.hospital.web.controller;

import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.domain.ResultVo;
import com.hkyl.hospital.entity.Dept;
import com.hkyl.hospital.entity.User;
import com.hkyl.hospital.mapper.custom.CustomDeptMapper;
import com.hkyl.hospital.service.DeptService;
import com.hkyl.hospital.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author sjh
 * @date 2023/07/08 17:22:07
 */
@RestController
@RequestMapping("dept")
public class DeptController {
    Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    @Autowired
    private CustomDeptMapper deptMapper;

    @Autowired
    private UserService userService;

    @PostMapping("save")
    public ResultVo save(@RequestBody Dept dept) {
        int rows = deptService.insertDept(dept);
        return rows > 0 ? ResultVo.success("添加成功", dept) : ResultVo.failure("添加失败");
    }

    @PostMapping("delete")
    public ResultVo delete(@RequestBody Map map) {
        int rows = 0;
        int num = 0;
        Integer deptId = (Integer) map.get("deptId");
        Dept dept = deptMapper.selectByPrimaryKey(deptId);
        List<Dept> depts = deptMapper.selectByExample(null);
        for (Dept dept1 : depts) {
            if (dept1.getParentId() == dept.getDeptId()) {
                num++;
            }
        }
        if (num == 0) {
            rows = deptService.deleteDept(Integer.valueOf(deptId));
        }
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.failure("删除失败");
    }

    @PostMapping("update")
    public ResultVo update(@RequestBody Dept dept) {
        int rows = deptService.updateDept(dept);
        return rows > 0 ? ResultVo.success("修改成功", dept) : ResultVo.failure("修改失败");
    }

    @PostMapping("select")
    public ResultVo select(@RequestBody Map map) {
        Integer pageNum = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        logger.info(String.valueOf(pageNum));
        logger.info(String.valueOf(pageSize));
        PageInfo<Dept> depts = new PageInfo<Dept>(deptService.selectDept(pageNum, pageSize));
        return ResultVo.success(depts);
    }

    @PostMapping("search")
    public ResultVo selectUserByDeptId(@RequestBody Map map) {
        String deptName = (String) map.get("deptName");
        Dept dept = deptMapper.selectOne(deptName);
        Integer deptId = dept.getDeptId();
        logger.info(String.valueOf(deptId));
        Integer pageNum = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<User> users = new PageInfo<>(userService.selectByDeptId(deptId, pageNum, pageSize));
        return ResultVo.success(users);
    }

    @PostMapping("selectAll")
    public ResultVo selectAll(){
        List<Dept> depts = deptService.selectDeptAll();
        return ResultVo.success(depts);
    }
}
